var Class = (function() {
  var create = function(propertries) {
    propertries = propertries || {}; //保证propertries不会是null或者是undefined
    //如果propertries返回真，则{}不执行；如果propertries返回假 则执行{}
    var clazz = propertries.hasOwnProperty("constructor")
      ? propertries.constructor
      : function() {};
    //如果我们设置的属性包含构造函数的定义，则clazz=构造函数
    //如果我们设置的属性不包含构造函数的定义，则clazz=function(){}
    implement.call(clazz, propertries); //处理prototype  //call 调用这个方法同时改变this的指针
    return clazz; //函数
  };

  var implement = function(propertries) {
    var proto = {},
      key,
      value;
    for (key in propertries) {
      value = propertries[key];
      if (classMarker.hasOwnProperty(key)) {
        classMarker[key].call(this, value);
      } else {
        proto[key] = value;
      }
    }
    mix(this.prototype, proto); //把属性的所有方法合并到这个函数的prototype
  };

  var createProto = (function(proto) {
    if (Object.__proto__) {
      return function(proto) {
        return { __proto__: proto };
      };
    } else {
      var ctor = function() {};
      return function(proto) {
        ctor.prototype = proto;
        return new ctor();
      };
    }
  })();

  var classMarker = {
    extends: function(parent) {
      var existed = this.prototype; //当前的对象prototype
      var temp = function() {}; //父类的临时变量
      var proto = createProto(parent.prototype); //{__proto__:父类的prototype}

      mix(this, parent);
      mix(proto, existed); //---子类：prototype
      proto.constructor = this;
      this.prototype = proto;
      this.superClass = parent.prototype;
    },
    mixes: function(items) {
      items instanceof Array || (items = [items]);
      var proto = this.prototype,
        item;
      while ((item = items.shift())) {
        mix(proto, item.prototype || item);
      }
    },
    statics: function(propertries) {
      mix(this, propertries);
    }
  };

  //混入 (jquery.extend)///mixins
  var mix = function(target) {
    for (var i = 1, len = arguments.length; i < len; i++) {
      var source = arguments[i],
        defineProps;
      for (var key in source) {
        var prop = source[key];
        if (prop) {
          if (typeof prop == "object") {//null 通过typeof也是返回object
            if (
              prop.value !== undefined ||
              typeof prop.get == "function" ||
              typeof prop.set == "function"
            ) {
              defineProps = defineProps || {};
              defineProps[key] = prop;
              continue;
            }
          }
          target[key] = prop;
        }
      }
      if (defineProps) Object.defineProperties(target, defineProps);
    }
    return target;
  };

  return { create: create, mix: mix };
})();
